package com.aplink.generic.constant;

import com.aplink.generic.util.Utility;

public enum SqlDataType {
    INTEGER( "Integer" ),
    REAL( "Real" ),
    TEXT( "Char" ),
    DATETIME( "Datetime" ),
    LONG(
            "Long" ),
    DOUBLE( "Double" );

    private String value;

    /**
     * @param value
     * @return return the datatype matches with the parameter, return null if
     * not found.
     */
    public static SqlDataType get( String value ) {
        for ( SqlDataType dataType : values() ) {
            if ( dataType.value.equalsIgnoreCase( value ) ) {
                return dataType;
            }
        }
        return null;
    }

    public Object parse( String value ) {
        if ( this.equals( INTEGER ) ) {
            return Integer.parseInt( value );
        } else if ( this.equals( DATETIME ) ) {
            return Utility.parseDatetime( value );
        } else if ( this.equals( LONG ) ) {
            return Long.parseLong( value );
        } else if ( this.equals( TEXT ) ) {
            return value;
        }
        return null;
    }

    private SqlDataType( String value ) {
        this.value = value;
    }

    public String getValue() {
        return this.value;
    }
}
